// 快慢指针，找到中间点，然后用分治的思想去处理中间点的左右两边
// 难点是处理边界问题，比如我的思路里，边界是左闭右开，所以链表的末尾指针 null 也包含进来

var sortedListToBST = function (head) {
  return makeTree(head, null);

  function makeTree (head, tail) {
    if (head == tail) return null;
    let fast = head,
        slow = head;
    while (fast !== tail) {
      fast = fast.next;
      if (fast !== tail) {
        slow = slow.next;
        fast = fast.next;
      }
    }
    let treeNode = new TreeNode (slow.val);
    treeNode.left = makeTree(head, slow);
    treeNode.right = makeTree(slow.next, tail);
    return treeNode;
  }
}